from django.core.cache import cache
from django.db import models

from utils.models import BaseModel


# Create your models here.


class NavCategory(BaseModel):
    """
    导航分类
    """

    name = models.CharField(max_length=20, verbose_name="导航名称")
    display_name = models.CharField(max_length=20, verbose_name="导航显示名称")
    weight = models.IntegerField(default=0, verbose_name="权重")
    external_link = models.URLField(verbose_name="外链地址", blank=True)

    class Meta:
        verbose_name = "导航分类"
        verbose_name_plural = verbose_name
        db_table = "tb_nav_category"
        ordering = ["weight"]

    def __str__(self):
        return self.name


class Kecheng(BaseModel):
    name = models.CharField(max_length=20, verbose_name="课程名称")
    level = models.IntegerField(verbose_name="课程等级")
    description = models.CharField(max_length=200, verbose_name="课程描述")
    parent = models.ForeignKey("self", null=True, blank=True, verbose_name="父级课程", on_delete=models.CASCADE)

    class Meta:
        verbose_name = "课程"
        verbose_name_plural = verbose_name
        db_table = "tb_kecheng"

    def __str__(self):
        return self.name


class Banner(BaseModel):
    title = models.CharField(max_length=50, verbose_name="标题")
    image = models.CharField(max_length=200, verbose_name="图片地址")
    link = models.CharField(max_length=200, verbose_name="跳转地址")
    note = models.CharField(max_length=200, verbose_name="备注")

    class Meta:
        verbose_name = "轮播图"
        verbose_name_plural = verbose_name
        db_table = "tb_banner"

    def __str__(self):
        return self.title

    def save(self, *args, **kwargs):

        super().save(*args, **kwargs)

        banner_key = "majing:home:banner_list"
        cache.delete(banner_key)

    def delete(self,*args, **kwargs):

        super().delete(*args, **kwargs)

        banner_key = "majing:home:banner_list"
        cache.delete(banner_key)

